/*
 * QTextBoxCustomizer.java
 *
 * Created on Jun 21, 2010, 9:19:58 PM
 */
package org.netbeans.modules.qcubedpalette.qtextbox;

import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.netbeans.modules.qcubedpalette.QControlCustomizer;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;

/**
 * QTextBoxCustomizer
 * The popup dialog that appears when the palette item is dragged into the
 * editor window.
 */
public class QTextBoxCustomizer extends javax.swing.JPanel implements QControlCustomizer {

	private Dialog dialog = null;
	private DialogDescriptor descriptor = null;
	private boolean dialogOK;
	QTextBox qtb;

	public QTextBoxCustomizer(QTextBox qtb) {
		this.qtb = qtb;

		initComponents();
	}

	@Override
	public boolean showDialog() {

		dialogOK = false;
		String displayName = "QTextBox";

		descriptor = new DialogDescriptor(this, "Insert " + displayName, true,
				DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION,
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						if (descriptor.getValue().equals(DialogDescriptor.OK_OPTION)) {
							evaluateInput();
							dialogOK = true;
						}
						dialog.dispose();
					}
				});

		dialog = DialogDisplayer.getDefault().createDialog(descriptor);
		dialog.setVisible(true);
		repaint();

		return dialogOK;
	}

	private void evaluateInput() {
		this.qtb.setRequired(chkRequired.isSelected());
		this.qtb.setName(txtName.getText());
		this.qtb.setText(txtText.getText());
		this.qtb.setTextMode(QTextMode.GetFromIndex(cbxTextMode.getSelectedIndex()));
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtText = new javax.swing.JTextField();
        chkRequired = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        cbxTextMode = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();

        jLabel1.setText(org.openide.util.NbBundle.getMessage(QTextBoxCustomizer.class, "QTextBoxCustomizer.jLabel1.text")); // NOI18N

        txtName.setText(org.openide.util.NbBundle.getMessage(QTextBoxCustomizer.class, "QTextBoxCustomizer.txtName.text")); // NOI18N

        jLabel2.setText(org.openide.util.NbBundle.getMessage(QTextBoxCustomizer.class, "QTextBoxCustomizer.jLabel2.text")); // NOI18N

        txtText.setText(org.openide.util.NbBundle.getMessage(QTextBoxCustomizer.class, "QTextBoxCustomizer.txtText.text")); // NOI18N

        chkRequired.setText(org.openide.util.NbBundle.getMessage(QTextBoxCustomizer.class, "QTextBoxCustomizer.chkRequired.text")); // NOI18N
        chkRequired.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkRequiredActionPerformed(evt);
            }
        });

        jLabel3.setText(org.openide.util.NbBundle.getMessage(QTextBoxCustomizer.class, "QTextBoxCustomizer.jLabel3.text")); // NOI18N

        cbxTextMode.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Single Line", "Multi Line", "Password" }));

        jLabel4.setText(org.openide.util.NbBundle.getMessage(QTextBoxCustomizer.class, "QTextBoxCustomizer.jLabel4.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(39, 39, 39)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel2)
                            .add(jLabel1)
                            .add(jLabel3)))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel4)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(chkRequired)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, cbxTextMode, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, txtText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, txtName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(txtName))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(txtText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(cbxTextMode, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(5, 5, 5)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(chkRequired)
                    .add(jLabel4))
                .add(20, 20, 20))
        );
    }// </editor-fold>//GEN-END:initComponents

	private void chkRequiredActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkRequiredActionPerformed
		// TODO add your handling code here:
	}//GEN-LAST:event_chkRequiredActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbxTextMode;
    private javax.swing.JCheckBox chkRequired;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtText;
    // End of variables declaration//GEN-END:variables
}
